package com.itheima.service.impl;


import com.itheima.anno.Log;
import com.itheima.mapper.DeptMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Dept;
import com.itheima.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    DeptMapper deptMapper;

    @Autowired
    EmpMapper empMapper;
    @Override
    public List<Dept> selectDepts() {
        List<Dept> deptList= deptMapper.selectDepts();
        return deptList;
    }

    /**
     * 删除部门
     *
     * @param id
     */
    @Override
    @Transactional
    @Log
    public void deleteDept(Integer id) {
        deptMapper.deleteDept(id);

        empMapper.deleteEmpById(id);
    }

    /**
     * 新增部门
     *
     * @param dept
     */
    @Override
    @Log
    public void insertDept(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.insertDept(dept);

    }

    /**
     * 数据回显
     *
     * @param id
     */
    @Override
    public Dept selectDeptById(Integer id) {
        Dept dept = deptMapper.selectDeptById(id);
        return dept;
    }

    /**
     * 修改部门名称
     *
     * @param dept
     */
    @Override
    @Log
    public void updateDeptName(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.updateDeptName(dept);
    }
}
